package com.wanqing.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.wanqing.constant.MessageConstant;
import com.wanqing.constant.StatusConstant;
import com.wanqing.dto.BookListDTO;
import com.wanqing.dto.BookListPageQueryDTO;
import com.wanqing.entity.BookBookList;
import com.wanqing.entity.BookList;
import com.wanqing.exception.DeletionNotAllowedException;
import com.wanqing.exception.BookListEnableFailedException;
import com.wanqing.mapper.BookMapper;
import com.wanqing.mapper.BookListMapper;
import com.wanqing.result.PageResult;
import com.wanqing.service.BookListService;
import com.wanqing.vo.BookItemVO;
import com.wanqing.vo.BookListVO;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import java.util.List;

/**
 * 书单服务实现类
 */
@Service
@Slf4j
public class BookListServiceImpl implements BookListService {

    @Resource
    private BookListMapper bookListMapper;

    @Resource
    private BookMapper bookMapper;

    /**
     * 分页查询书单信息
     *
     * @param bookListPageQueryDTO 分页查询DTO，包含页码和页面大小等信息
     * @return 返回分页结果对象，包含总记录数和数据列表
     */
    @Override
    public PageResult pageQuery(BookListPageQueryDTO bookListPageQueryDTO) {
        // 开始分页
        PageHelper.startPage(bookListPageQueryDTO.getPage(), bookListPageQueryDTO.getPageSize());
        // 执行分页查询
        Page<BookListVO> page = bookListMapper.pageQuery(bookListPageQueryDTO);
        // 返回分页结果
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 保存书单及其关联的图书
     *
     * @param bookListDTO 书单DTO，包含书单信息及其关联的图书信息
     */
    @Override
    @Transactional
    public void saveWithBook(BookListDTO bookListDTO) {
        // 将DTO转换为实体对象
        BookList bookList = new BookList();
        BeanUtils.copyProperties(bookListDTO, bookList);
        // 保存书单信息
        bookListMapper.save(bookList);
        // 获取关联的图书信息
        List<BookBookList> bookBookList = bookListDTO.getBookBookList();
        if (bookBookList != null && !bookBookList.isEmpty()) {
            for (BookBookList bookListBook : bookBookList) {
                // 设置书单ID
                bookListBook.setBookListId(bookList.getId());
            }
            // 批量保存书单图书关联信息
            bookListMapper.saveBookBookListBatch(bookBookList);
        }
    }

    /**
     * 根据ID查询书单及其关联的图书信息
     *
     * @param id 书单ID
     * @return 返回书单VO对象，包含书单信息及其关联的图书信息
     */
    @Override
    public BookListVO getByIdWithBook(Long id) {
        // 初始化书单VO对象
        BookListVO bookListVO = new BookListVO();
        // 查询书单信息
        BookList bookList = bookListMapper.getById(id);
        // 查询关联的图书信息
        List<BookBookList> bookes = bookListMapper.getBookByBookListId(id);
        // 将书单信息复制到书单VO对象中
        BeanUtils.copyProperties(bookList, bookListVO);
        // 设置关联的图书信息
        bookListVO.setBookBookList(bookes);
        return bookListVO;
    }

    /**
     * 更新书单及其关联的图书信息
     *
     * @param bookListDTO 书单DTO，包含更新后的书单信息及其关联的图书信息
     */
    @Override
    public void updateWithBook(BookListDTO bookListDTO) {
        // 将DTO转换为实体对象
        BookList bookList = new BookList();
        BeanUtils.copyProperties(bookListDTO, bookList);
        // 更新书单信息
        bookListMapper.update(bookList);
        // 删除原有的书单图书关联信息
        bookListMapper.deleteBookByBookListId(bookList.getId());
        // 获取更新后的关联图书信息
        List<BookBookList> bookBookList = bookListDTO.getBookBookList();
        if (bookBookList != null && !bookBookList.isEmpty()) {
            for (BookBookList bookListBook : bookBookList) {
                // 设置书单ID
                bookListBook.setBookListId(bookList.getId());
            }
            // 批量保存更新后的书单图书关联信息
            bookListMapper.saveBookBookListBatch(bookBookList);
        }
    }

    /**
     * 启用或禁用书单
     *
     * @param status 书单状态，启用或禁用
     * @param id     书单ID
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        // 如果是启用操作
        if (status.equals(StatusConstant.ENABLE)) {
            // 检查书单关联的图书是否都已启用
            List<BookBookList> bookes = bookListMapper.getBookByBookListId(id);
            if (bookes != null && !bookes.isEmpty()) {
                for (BookBookList book : bookes) {
                    Integer bookStatus = bookMapper.getById(book.getBookId()).getStatus();
                    if (bookStatus.equals(StatusConstant.DISABLE)) {
                        // 如果有未启用的图书，抛出异常
                        throw new BookListEnableFailedException(MessageConstant.BOOKLIST_ENABLE_FAILED);
                    }
                }
            }
        }

        // 更新书单状态
        BookList bookList = BookList.builder().id(id).status(status).build();
        bookListMapper.update(bookList);
    }

    /**
     * 批量删除书单
     *
     * @param ids 书单ID列表
     */
    @Override
    public void deleteBatch(List<Long> ids) {
        // 检查书单是否已启用，如果已启用，则不允许删除
        for (Long id : ids) {
            BookList bookList = bookListMapper.getById(id);
            if (bookList.getStatus().equals(StatusConstant.ENABLE)) {
                throw new DeletionNotAllowedException(MessageConstant.BOOKLIST_ON_SALE);
            }
        }
        // 删除书单关联的图书信息
        bookListMapper.deleteBookBatch(ids);
        // 批量删除书单信息
        bookListMapper.deleteBatch(ids);
    }

    /**
     * 条件查询
     *
     * @param bookList
     * @return
     */
    public List<BookList> list(BookList bookList) {
        List<BookList> list = bookListMapper.list(bookList);
        return list;
    }

    /**
     * 根据id查询图书选项
     *
     * @param id
     * @return
     */
    public List<BookItemVO> getBookItemById(Long id) {
        return bookListMapper.getBookItemByBookListId(id);
    }
}
